package com.web.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ClientTask implements Runnable {

	private Socket client;
	private BufferedReader reader;
	private BufferedWriter out;

	public ClientTask(Socket client) {
		this.client = client;
	}

	@Override
	public void run() {
		try {
			String ip = client.getInetAddress().getHostAddress();
			/* 接收请求信息 */
			reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "GBK"));
			// 读取第一行，获取消息头中的请求地址跟请求方式(GET | POST)
			String line = reader.readLine();
			System.out.println(line);
			String resource = ""; // 请求地址
			String method = ""; // 请求方式
			if (line != null && !"".equals(line)) {
				resource = line.substring(line.indexOf("/"), line.lastIndexOf("/") - 5);
				method = getMethod(line);
			}
			System.out.println("resource=" + resource);
			System.out.println("method=" + method);
			//
			int contentLength = 0; // POST数据长度
			while ((line = reader.readLine()) != null) {
				if ("".equals(line))
					break;
				// System.out.println(line);
				if (line.startsWith("Content-Length")) { // 取得Content-Length
					contentLength = Integer.parseInt(line.substring(line.indexOf(':') + 1).trim());
				}
			}
			// 取得POST表单提交的内容
			String content = "";
			if (contentLength > 0 && method.equals("POST")) {
				System.out.println("POST -------------------- POST");
				char[] buf = new char[contentLength];
				for (int i = 0; i < contentLength; i++) {
					buf[i] = (char) reader.read();
				}
				content = new String(buf);
				System.out.print(URLDecoder.decode(content, "GBK"));
				System.out.println();
			}

			System.out.println("请求 -------------------- 结束\n");
			/*
			 * 在这可以进行判断(resource)，默认发送以下数据(服务器主页)。 否则要什么给什么，没有就404
			 */
			/* 发送消息给客户端 */
			out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "GBK"));
			if (!resource.equals("/")) { // 不是默认资源
				File file = new File("src/webapps" + resource); // 资源文件
				System.out.println(file.getAbsolutePath());
				if (file != null && file.exists()) {
					BufferedReader bufReader = new BufferedReader(
							new InputStreamReader(new FileInputStream(file), "GBK"));
					String req_resource = null;
					while ((req_resource = bufReader.readLine()) != null) {
						out.write(req_resource);
					}
					bufReader.close();
				} else { // 请求资源不存在，报404
					out.write("404:出错啦！！！！！！！出门左拐，回到服务器主页");
				}
			} else {
				out.write("<html>");
				out.write("<head>");
				out.write("<title>");
				out.write("我的网站");
				out.write("</title>");
				out.write("</head>");
				out.write("<body>");
				out.write("<center><h1>你好，" + ip + "，欢迎来访</h1></center>");
				out.write("<form action='#' method='post'>");
				out.write("用户名：");
				out.write("<input type='text' name='username' /><br/><br/>");
				out.write("密&nbsp;码：");
				out.write("<input type='password' name='password' /><br/>");
				out.write("<input type='submit' value='提交' />");
				out.write("</form>");
				if (method.equals("POST") && !"".equals(content)) { // POST请求，并且表单有数据
					setPostValue(content); // 保存进Map集合
					out.write("您的用户名是：" + URLDecoder.decode(getPostValue("username"), "GBK") + "\t密码是： "
							+ URLDecoder.decode(getPostValue("password"), "GBK"));
				}
				out.write("</body>");
				out.write("</html>");
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null)
					out.close();
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private Map<String, String> map; // 存放POST键值对信息

	/**
	 * 保存键值对信息到Map集合
	 * 
	 * @param content
	 *            原始POST消息
	 */
	private void setPostValue(String content) {
		// String urlRegex = "(^|&|\\?)"+ name + "=([^&]*)(&|$)";
		// username=1111&password=111
		String[] keyValues = content.split("&");
		map = new HashMap<String, String>();
		for (String s : keyValues) {
			String[] keyValue = s.split("="); // username=1111
			map.put(keyValue[0], keyValue[1]);
		}
	}

	/**
	 * 获取参数值
	 * 
	 * @param name
	 *            参数名
	 * @return 参数值
	 */
	private String getPostValue(String name) {
		if (map != null && !map.isEmpty())
			return map.get(name);
		return "";
	}

	/**
	 * 获取请求方式
	 * 
	 * @param line
	 *            消息头第一行
	 * @return POST | GET
	 */
	private String getMethod(String line) {
		Pattern pattern = Pattern.compile("^(GET|POST)\\b");
		Matcher m = pattern.matcher(line);
		if (m.find())
			return m.group();
		return "";
	}

}
